﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using BillingSystem.DataSetMainTableAdapters;

namespace BillingSystem
{
    public partial class FormShowAllBalances : Form
    {
        DataSetMain dataSet;
        BALANCETableAdapter t_adapter = new BALANCETableAdapter();
        BindingSource bindingSource;

        //DataGridViewTextBoxColumn twoDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        //DataGridViewTextBoxColumn threeDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        //DataGridViewTextBoxColumn fourDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        //DataGridViewTextBoxColumn oneDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn NUMBERDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn MONEYDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn FIODataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
        DataGridViewTextBoxColumn ACCOUNT_NUMBERDataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();

        public FormShowAllBalances()
        {
            InitializeComponent();
        }

        private void InitGrid(BindingSource bs)
        {

            MONEYDataGridViewTextBoxColumn.DataPropertyName = "MONEY";
            MONEYDataGridViewTextBoxColumn.HeaderText = "Бабосы";
            MONEYDataGridViewTextBoxColumn.Name = "dURATIONDataGridViewTextBoxColumn";
            MONEYDataGridViewTextBoxColumn.ReadOnly = true;

            FIODataGridViewTextBoxColumn.DataPropertyName = "FIO";
            FIODataGridViewTextBoxColumn.HeaderText = "ФИО";
            FIODataGridViewTextBoxColumn.Name = "cOSTDataGridViewTextBoxColumn";
            FIODataGridViewTextBoxColumn.ReadOnly = true;

            //CLIENT_IDDataGridViewTextBoxColumn.DataPropertyName = "CLIENT_ID";
            //CLIENT_IDDataGridViewTextBoxColumn.HeaderText = "Номер";
            //CLIENT_IDDataGridViewTextBoxColumn.Name = "NUMBERDataGridViewTextBoxColumn";
            //CLIENT_IDDataGridViewTextBoxColumn.ReadOnly = true;

            ACCOUNT_NUMBERDataGridViewTextBoxColumn.DataPropertyName = "ACCOUNT_NUMBER";
            ACCOUNT_NUMBERDataGridViewTextBoxColumn.HeaderText = "№ Договора";
            ACCOUNT_NUMBERDataGridViewTextBoxColumn.Name = "NUMBERDataGridViewTextBoxColumn";
            ACCOUNT_NUMBERDataGridViewTextBoxColumn.ReadOnly = true;

            dataGridView1.MultiSelect = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.AutoGenerateColumns = false;

            dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                FIODataGridViewTextBoxColumn,
                ACCOUNT_NUMBERDataGridViewTextBoxColumn,
                MONEYDataGridViewTextBoxColumn
            });
            dataGridView1.DataSource = bs;
            dataGridView1.AutoResizeColumns();
        }


        public bool ShowForm(DataSetMain dataSet)
        {
            this.dataSet = dataSet;
            t_adapter.Fill(dataSet.BALANCE);
            bindingSource = new BindingSource(dataSet, "BALANCE");
            InitGrid(bindingSource);

            if (ShowDialog() == DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (bindingSource.Current == null) return;

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.InitialDirectory = "C:\\";
            saveFileDialog.Filter = "XML files (*.xml)|*.xml";
            saveFileDialog.FilterIndex = 1;
            saveFileDialog.RestoreDirectory = true;

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                bindingSource.EndEdit();
                XMLBoss xmlboss = new XMLBoss();
                xmlboss.WriteXml(dataSet.BALANCE, saveFileDialog.FileName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            

            FolderBrowserDialog dlg = new FolderBrowserDialog();

            dlg.SelectedPath = "C:\\";
            //dlg.Filter = "XML files (*.xml)|*.xml";
            //dlg.FilterIndex = 1;
            //dlg.RestoreDirectory = true;

            bool res;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                bindingSource.MoveFirst();
                for (int i = 0; i < bindingSource.Count; i++)
                {
                    DataRowView drv = (DataRowView)bindingSource.Current;
                    decimal num = (decimal)drv["ACCOUNT_NUMBER"];

                    res = iNet.SaveToFile("http://www.billing.int/index/getdraft/" + num + "/", dlg.SelectedPath + "\\" + num + ".jpg");
                    if (!res)
                        MessageBox.Show("WTF?!" + (iNet.WasException ? "\n\n" + iNet.LastException : ""));
                    bindingSource.MoveNext();

                }
            }
        }
    }
}
